//--navigation panel expand/collapse
function resizeMe(windowHeight, windowWidth) {
	//Standard height, for which the body font size is correct
	var row2Height = windowHeight	- $("#headerRow").height() - 2;
	$("#leftColumn").height(row2Height - $("#nav_panel_buttons_table").height());
	$("#rightColumn").height(row2Height);
	$("#rightColumn").width(windowWidth - $("#leftMenuBar").width() - 2);
	resize_grid(jQuery("#s3list"));
}

jQuery(document).ready(function($) {
	$("#showHeaderButton").hide();
	$(window).bind('resize', function() {
		resizeMe($(window).height(), $(window).width());
	}).trigger("resize");

	resizeMe($(window).height(), $(window).width());
	
	$("#leftnav").treeview({
		animated: "fast",
		collapsed: true,
		persist: "cookie"
	});

	
	$("#nav_panel_collapse").click(function() {
		var windowWidth = $(window).width();
		panelCollapseExpend();
		$("#leftColumn").width('auto');
		$("#nav_panel_buttons_table").width('auto');
		$("#rightColumn").width(windowWidth - $("#leftMenuBar").width() - 5);
		resize_grid(jQuery("#s3list"));
	});

	$("#nav_panel_expend").click(function() {
		var windowWidth = $(window).width();
		panelCollapseExpend();
		$("#leftColumn").width(220);
		$("#nav_panel_buttons_table").width(220);
		$("#rightColumn").width(windowWidth - $("#leftColumn").width() - 5);
		resize_grid(jQuery("#s3list"));
	});

	$("#nav_panel_reduce").mousehold(function(){
		if($("#leftColumn").width()>48){
			$("#leftColumn").width("-=20px");
			$("#nav_panel_buttons_table").width("-=20px");
			$("#rightColumn").width("+=20px");
			resize_grid(jQuery("#s3list"));
		}
	});
	$("#nav_panel_enlarge").mousehold(function(){
		$("#leftColumn").width("+=20px");
		$("#nav_panel_buttons_table").width("+=20px");
		$("#rightColumn").width("-=20px");
		resize_grid(jQuery("#s3list"));
	});

	$("#hideHeaderButton").click(function(){
		var windowHeight = $(window).height();
		var windowWidth = $(window).width();
		$("#headerLine1").hide();
		$("#headerLine2").hide();
		$("#hideHeaderButton").hide();
		$("#showHeaderButton").show();
		resizeMe(windowHeight, windowWidth);
	});
	$("#showHeaderButton").click(function(){
		var windowWidth = $(window).width();
		var windowHeight = $(window).height();
		$("#headerLine1").show();
		$("#headerLine2").show();
		$("#hideHeaderButton").show();
		$("#showHeaderButton").hide();
		
		resizeMe(windowHeight, windowWidth);
	});
	
	function panelCollapseExpend() {
		$("#nav_panel_reduce").toggle();
		$("#nav_panel_enlarge").toggle();
		$("#nav_panel_expend").toggle();
		$("#nav_panel_collapse").toggle();
		$("#leftColumn").toggle();
	}
//	$("ul#leftnav").find("a[href*='"+window.location.pathname+'?'+window.location.search.substring(1)+"']").each(function(){
	$("ul#leftnav").find("a[href*='"+window.location.pathname+"']").each(function(){
		var pattern = /;jsessionid=.*\?/;
		var leftNavLink = $(this).attr("href");
		//removing jseesionid
		leftNavLink= leftNavLink.replace(pattern,"?");
		var queryStringParam = window.location.search.substring(1);
		var leftNavLinkQueryParam=leftNavLink.substring(leftNavLink.indexOf("?")+1);
		if(leftNavLinkQueryParam.search(queryStringParam)>-1){
			$(this).addClass("selected");
		}
	});
	//confirmation before opening configuration
	$('#confirm').bind('click', function(e) {
		var $clicked = $(e.target);
		if($clicked.prev('div.hitarea').hasClass('collapsable-hitarea')){
			confirmFor(this);
		}
    });

	$('#confirm').prev('div.hitarea').bind('click', function(e) {
		if($(this).hasClass('collapsable-hitarea')){
			confirmFor(this);
		}
    });

	function confirmFor(obj){
		if(!window.confirm("Achtung ! \nAenderung von globalen Einstellungen f�r alle User (Logging aktiv) !\n Wollen Sie wirklich fortfahren ?")){
			$(obj).click();
		}
	}
});

function openWindow(link) {
	try {
		window.open(link);
	} catch (e) {

	}
}

function openConfiguration()
{
	var configList = document.getElementById('configurationList');
	
	if (configList.style.display == 'none')	
		{
			$("#configIcon").addClass("hitarea_deactiv1").removeClass("hitarea expandable-hitarea");
		}
	else
		{
			$("#configIcon").addClass("hitarea expandable-hitarea").removeClass("hitarea_deactiv1");
		}
	$("#configurationList").slideToggle('fast','linear');
}

var link = "";
function getCurrentPageLink()
{
	//get current page link
	var splittedLinks = document.URL.split('/');
	var i;
	for ( i=splittedLinks.length-1; i>=0;--i)
		if (splittedLinks[i].lastIndexOf('.do')>-1)
			{
				link = splittedLinks[i];
				break;
			}
	if (link != "")
		{
			link = link.split('.')[0];
		}
}

function isCurrentInSubMenu()
{
	if (link == "")
		getCurrentPageLink();
	// get list of sub-menu link element
	subMenuElement = document.getElementById('configurationList').getElementsByTagName("a");
	var i;
	for (i = 0 ; i< subMenuElement.length; ++i)
		{
			var subLink = subMenuElement[i].name;
			if (subLink.match(link))
			{
				return true;
			}
		}
	return false;
}

function expandSubMenu()
{
	if (isCurrentInSubMenu())
		{
			$("#configurationList").show();
			$("#configIcon").addClass("hitarea_deactiv1").removeClass("hitarea expandable-hitarea");
		}
}

function selectMenu()
{
	if (link == "")
		getCurrentPageLink();
	subMenuElement = document.getElementById('leftnav').getElementsByTagName("a");
	var i;
	for (i = 0 ; i< subMenuElement.length; ++i)
		{
			var subLink = subMenuElement[i].name;
			if (subLink.match(link) && subLink.length == link.length )
			{
			 	subMenuElement[i].className = "selected";
				break;
			}
		}
}

function writeLocation()
{
	var boschBread 		= document.getElementById('boschBread');
	var formatStart 	= '<a class="breadcrumb-Act" href="javascript:void(0);">';
	var formatEnd 		= '</a>';
	var configurationString = 'Configuration';
	var configurationMenu 	= formatStart + configurationString 	+ formatEnd;
	var documentTitle 		= formatStart+ document.title		+ formatEnd;
	if (isCurrentInSubMenu())
		{
			boschBread.innerHTML = configurationMenu +  documentTitle;
		}
	else
		{
		boschBread.innerHTML = documentTitle;
		}
}

function selectExternalLink() {
	$('.externalFile').click(function(event) {
//		if ($.browser.msie) { // Internet Explorer
//			event.target.click();
//		} else if ($.browser.mozilla || $.broswer.opera) { // FireFox
			
			var externalLink = event.target.href;
			
			// don't check http or https protocal
			if (externalLink.indexOf("http://") != 0 
					&& externalLink.indexOf("https://") != 0) {
				externalLink = "openLink.do?fileLink=" + externalLink;
				window.open(externalLink);
				return false;
			} else {
				return true;
			}
			
			//event.target.href = externalLink;
			//event.target.click();
//		}
	});
}
